package org.jinyan.db.utils;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;

import org.apache.log4j.Logger;
import org.jinyan.utils.CommonUtils;

public class ConnectionUtil {
	private static Logger logger = Logger.getLogger(ConnectionUtil.class);

	public static Connection getConn() {
		System.out.println("获取数据库连接start");
		logger.info("获取数据库连接 info start");
		String classDriverName = "com.mysql.jdbc.Driver";
		/*
		 * String user = "rf9qx7749qal31p0"; String password = "elesqlroot";
		 * String url =
		 * "jdbc:mysql://rdsyqqneyvybv6n.mysql.rds.aliyuncs.com:3306/rf9qx7749qal31p0"
		 * ;
		 */

		String user = "y6HVy8Z2D1G6Ly67mj2VDkcG";
		String password = "DVesGGwMpMlIamFKU3PZRRpkdygkDuRQ";
		String url = "jdbc:mysql://sqld.duapp.com:4050/GIpOIhjZbnuaDnfobwOz";

		// String user = "1z5kjx00nm";
		// String password = "xzkyxxjjm20yzkxyz2hkh30jxx3lwixlxj1514ji";
		// String url =
		// "jdbc:mysql://w.rdc.sae.sina.com.cn:3307/app_wechartled";
		Connection conn = null;

		Map<String, String> map = null;

		 try {
			String path = CommonUtils
					.getPackagePath("/etc/dataAccessContext.xml");
			// String path =System.getProperty("user.dir")
			// + "/src/etc/dataAccessContext.xml";
			System.out.println("读取数据库配置文件路径" + path);
			logger.info("读取数据库配置文件路径" + path);
			map = CommonUtils.readAllPropertiesToMap(path);
		} catch (IOException e1) {
			System.out.println("读取数据库配置文件失败" + e1.getLocalizedMessage());
			logger.error("读取数据库配置文件失败", e1);
			throw new RuntimeException(e1);
		} 

		if (map != null) {
			if (map.containsKey("classDriverName")) {
				classDriverName = map.get("classDriverName");
			}
			if (map.containsKey("user")) {
				user = map.get("user");
			}
			if (map.containsKey("password")) {
				password = map.get("password");
			}
			if (map.containsKey("url")) {
				url = map.get("url");
			}

		}

		try {
			Class.forName(classDriverName);
			conn = DriverManager.getConnection(url, user, password);
		} catch (ClassNotFoundException e) {
			System.out.println("读取数据库连接失败,获取驱动类失败" + e.getLocalizedMessage());
			logger.error("读取数据库连接失败,获取驱动类失败", e);
			e.printStackTrace();
		} catch (SQLException e) {
			System.out.println("读取数据库连接失败,获取数据失败" + e.getLocalizedMessage());
			logger.error("读取数据库连接失败,获取数据失败", e);
			e.printStackTrace();
		}

		return conn;
	}

	public static void closeAll(Statement stmt, ResultSet rs) {

		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				logger.error("数据库ResultSet关闭失败:", e);
				e.printStackTrace();
			}
		}
		rs = null;
		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		stmt = null;

	}

	public static void closeStatement(Statement stmt) {
		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				logger.error("数据库Statement关闭失败:", e);
				e.printStackTrace();
			}
		}
		stmt = null;
	}

	public static void closeStatement(Statement stmt, ResultSet rs) {

		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				logger.error("数据库ResultSet关闭失败:", e);
				e.printStackTrace();
			}
		}
		rs = null;
		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				logger.error("数据库Statement关闭失败:", e);
				e.printStackTrace();
			}
		}
		stmt = null;

	}

	public static void closePreparedStatement(PreparedStatement pstmt) {

		if (pstmt != null) {
			try {
				pstmt.close();
			} catch (SQLException e) {
				logger.error("数据库PreparedStatement关闭失败:", e);
				e.printStackTrace();
			}
		}
		pstmt = null;

	}

	public static void closePreparedStatement(PreparedStatement pstmt,
			ResultSet rs) {

		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				logger.error("数据库ResultSet关闭失败:", e);
				e.printStackTrace();
			}
		}
		rs = null;
		if (pstmt != null) {
			try {
				pstmt.close();
			} catch (SQLException e) {
				logger.error("数据库PreparedStatement关闭失败:", e);
				e.printStackTrace();
			}
		}
		pstmt = null;
	}

}
